package com.example.websocket;

import com.example.websocket.client.MyWebSocketClient;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.TextMessage;

import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Application {
    public static void main(String[] args) {
        MyWebSocketClient client = new MyWebSocketClient();
        try {
            client.connect("ws://101.251.216.46:8004/ws/audio/recognize"); // 替换为你的WebSocket服务器地址和端口
            TextMessage textMessage = new TextMessage("Hello, WebSocket!");

            String filePath = "/Users/haojq/Documents/trs-folder/eng-voice.wav"; // 替换为实际的文件路径
            FileInputStream fis = new FileInputStream(filePath);
             FileChannel channel = fis.getChannel();
            // 获取文件大小
            long fileSize = channel.size();
            // 创建对应大小的 ByteBuffer
            ByteBuffer buffer = ByteBuffer.allocate((int) fileSize);
            // 将文件内容读取到 ByteBuffer 中
            channel.read(buffer);
            // 切换 ByteBuffer 为读模式
            buffer.flip();
            BinaryMessage binaryMessage = new BinaryMessage(buffer);

            client.sendMessage(binaryMessage); // 发送消息到服务器
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
